package com.ruoyi.project.manhua.chapter.controller;

import java.util.LinkedHashMap;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.manhua.chapter.service.IMhChapterService;
import com.ruoyi.project.manhua.climb.service.IMhClimbService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.manhua.chapter.domain.MhChapter;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 漫画章节 Controller
 * 
 * @author yuan
 * @date 2021-01-13
 */
@Controller
@RequestMapping("/manhua/chapter")
public class MhChapterController extends BaseController
{
    private String prefix = "manhua/chapter";

    @Autowired
    private IMhChapterService mhChapterService;

    @Autowired
    private IMhClimbService mhClimbService;


    @RequiresPermissions("manhua:chapter:view")
    @GetMapping("/{id}")
    public String chapter(@PathVariable("id")Long id,ModelMap map)
    {
        map.put("MhNameDataAll",  mhClimbService.mapMhNames());
        map.put("nameId",id);
        return prefix + "/chapter";
    }

    /**
     * 查询漫画章节 列表
     */
    @RequiresPermissions("manhua:chapter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MhChapter mhChapter)
    {

        startPage();
        List<MhChapter> list = mhChapterService.selectMhChapterList(mhChapter);

        System.err.println(list.size());
        return getDataTable(list);
    }

    /**
     * 查询漫画章节 列表
     */
    @RequiresPermissions("manhua:chapter:list")
    @GetMapping("/getlist/{getNameId}")
    @ResponseBody
    public TableDataInfo getList(@PathVariable("getNameId")String getNameId)
    {
        MhChapter mhChapter = new MhChapter();
        /*不等于空*/
        if(StringUtils.isNotEmpty(getNameId)){
            if(!getNameId.split(",")[1].equals("无名称")) {
                /*漫画id*/
                mhChapter.setNameId(Long.valueOf(getNameId.split(",")[0]));
            }
        }
        startPage();
        List<MhChapter> list = mhChapterService.selectMhChapterList(mhChapter);
        return getDataTable(list);
    }


    /**
     * 查询漫画章节 列表
     */
    @RequiresPermissions("manhua:chapter:list")
    @PostMapping("/selectList")
    @ResponseBody
    public AjaxResult selectList(MhChapter mhChapter)
    {
        return AjaxResult.success(mhChapterService.selectMhChapterList(mhChapter));
    }

    /**
     * 导出漫画章节 列表
     */
    @RequiresPermissions("manhua:chapter:export")
    @Log(title = "漫画章节 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MhChapter mhChapter)
    {

        List<MhChapter> list = mhChapterService.selectMhChapterList(mhChapter);
        ExcelUtil<MhChapter> util = new ExcelUtil<MhChapter>(MhChapter.class);
        return util.exportExcel(list, "chapter");
    }

    /**
     * 新增漫画章节 
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存漫画章节 
     */
    @RequiresPermissions("manhua:chapter:add")
    @Log(title = "漫画章节 ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MhChapter mhChapter)
    {
        return toAjax(mhChapterService.insertMhChapter(mhChapter));
    }

    /**
     * 修改漫画章节 
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        MhChapter mhChapter = mhChapterService.selectMhChapterById(id);
        mmap.put("mhChapter", mhChapter);
        return prefix + "/edit";
    }

    /**
     * 修改保存漫画章节 
     */
    @RequiresPermissions("manhua:chapter:edit")
    @Log(title = "漫画章节 ", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MhChapter mhChapter)
    {
        return toAjax(mhChapterService.updateMhChapter(mhChapter));
    }

    /**
     * 删除漫画章节 
     */
    @RequiresPermissions("manhua:chapter:remove")
    @Log(title = "漫画章节 ", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mhChapterService.deleteMhChapterByIds(ids));
    }
}
